<?php 		
class CCmsController extends CController{

	protected $m_arrobjPages;
	protected $m_objPage;
	protected $m_objFCKeditor;
	//protected $m_arrobjAdvertisementTypes;		
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();		
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'list':
				$this->handleViewPageList();
				break;
				
			case 'add':									
			case 'edit':
				$this->handleAddEditPage();
				break;
				
			case 'insert_update_page':
				$this->handleInsertUpdatePage();
				break;
			
			case 'update_page_action':
				$this->handleUpdatePageAction();
				break;
			
			case 'view':
				$this->handleViewPage();
				break;
				
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewPageList() {
		$this->m_arrobjPages = CPages::fetchPages( $this->m_objDatabase );	
		$this->displayPageList();
	}
	
	function handleAddEditPage() {
		
		include( "Liabraries/fckeditor/fckeditor.php");
		
		$this->m_objFCKeditor = new FCKeditor('page[description]') ;
		$this->m_objFCKeditor->BasePath = SITE_PATH . '/Liabraries/fckeditor/';		
		$this->m_objFCKeditor->Width  = '100%';
		$this->m_objFCKeditor->Height = '500';
		
		$this->m_objPage = new CPage();
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objPage = CPages::fetchPageById( $_REQUEST['id'], $this->m_objDatabase );
			$this->m_objFCKeditor->Value  = $this->m_objPage->getDescription();
		}
				
		$this->displayAddEditPage();
	}

	function handleViewPage() {
		
		$intPageId = $this->m_objData->getRequestData( array( 'id' ) );		
		if( true == isset( $intPageId ) ) {
			$this->m_objPage = CPages::fetchPageById( $intPageId, $this->m_objDatabase );
		}	
		$this->displayViewPage();
	}
	
	function handleInsertUpdatePage() {
			
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'page' ) );
				
				$this->m_objPage = CPages::setValues( 'CPage', $arrmixFieldsValues );
				//display($arrmixAdvertisementValues);die;
				//$this->m_objAdvertisement = CAdvertisements::setValues( 'CAdvertisement', $arrmixAdvertisementValues );
				
				if( false == $this->m_objPage->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				}
				
				if( true == isset( $arrmixFieldsValues['id'] ) ) {
				
					if( false == $this->m_objPage->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update page.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Page update successfully. ' );
					}
				} else {
				
					if( false == $this->m_objPage->insert( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create page.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Page created successfully. ' );
					}
				}
				
				redirect( '/admin/cms' );
		}
 		redirect( '/admin/cms/add' );
	}
	
	function handleUpdatePageAction() {
		
		switch( true ) {
			default:
				
				$strAction = $this->m_objData->getPostData( array( 'page', 'action' ) );				
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
				
				$arrintPageIds = $this->m_objData->getPostData( array( 'page', 'id' ) );
				if( false == valArr( $arrintPageIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
								
				$this->m_objPage = new CPage();
				if( false == valObj( $this->m_objPage, 'CPage' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
				
				if( 'delete' == $strAction ) {
					if( true == $this->m_objPage->deletePagesByIds( $arrintPageIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objPage->updatePagesActionByIds( $strAction, $arrintPageIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
									
		}
		redirect( '/admin/cms' );
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayPageList() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'pages', $this->m_arrobjPages );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/cms/view_page_list.tpl');
	}
	
	function displayAddEditPage() {
		$this->display();
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'page', $this->m_objPage );
		//$this->m_objSmarty->assign_by_ref( 'advertisement_types', $this->m_arrobjAdvertisementTypes );
		
		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/cms/view_add_edit_page.tpl' );
	}
	
	function displayViewPage() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'page', $this->m_objPage );		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/cms/view_page.tpl');

	}
	
	function display() {
		
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'content_pages' );
	}
}
?>